package com.shushan.shushanbao.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.shushan.base.Constants;
import com.shushan.service.DialRTAService;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.util.SharedPreferencesUtils;
import com.shushan.util.UmengEventUtil;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * @ClassName: WXEntryActivity
 * @Desciption: 微信分享回调
 * @author: yichaohua
 * @date: 2018-09-10
 */
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "WXEntryActivity";

    private IWXAPI api;
    private BabyApplication babyApplication = BabyApplication.getInstance();
    public static int type = 1;//分享类型 1：助学分享  2：优惠券分享 3：邀请码分享

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.i(TAG,"onCreate()");

        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID,false);
        api.handleIntent(getIntent(), this);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        Log.i(TAG,"onNewIntent()");

        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {
        Log.i(TAG,"onReq()");
    }

    @Override
    public void onResp(BaseResp baseResp) {
        Log.i(TAG,"onReq() errCode=" + baseResp.errCode);

        switch (baseResp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                onShareEvent(true,type,"ERR_OK");
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                onShareEvent(false,type,"ERR_USER_CANCEL");
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                onShareEvent(false,type,"ERR_AUTH_DENIED");
                break;
            default:
                break;
        }

        boolean isChildControl = SharedPreferencesUtils.getBooleanDate(Constants.IS_CHILD_CONTROL_SHAREPREFERS_KEY);
        if(isChildControl){
            Intent intent1 = new Intent(this,DialRTAService.class);
            stopService(intent1);
        }
        finish();
    }

    private void onShareEvent(boolean isSuccess, int type, String info) {
        if(isSuccess){
            switch (type){
                case 1:
                    UmengEventUtil.onEvent(babyApplication,"invite_friends_success_id",info);
                    break;
                case 2:
                    UmengEventUtil.onEvent(babyApplication,"coupon_share_success_id",info);
                    break;
                case 3:
                    UmengEventUtil.onEvent(babyApplication,"invite_1000_share_success_id",info);
                    break;
                default:
                    break;
            }
        }else {
            switch (type){
                case 1:
                    UmengEventUtil.onEvent(babyApplication,"invite_friends_faild_id",info);
                    break;
                case 2:
                    UmengEventUtil.onEvent(babyApplication,"coupon_share_faild_id",info);
                    break;
                case 3:
                    UmengEventUtil.onEvent(babyApplication,"invite_1000_share_faild_id",info);
                    break;
                default:
                    break;
            }
        }
    }
}
